from typing import AsyncGenerator
from redis.asyncio import Redis,ConnectionPool

_pool = ConnectionPool|None

def init_redis():
    global _pool
    _pool = ConnectionPool.from_url(
    "redis://10.0.16.213",
    max_connections=10,
    decode_responses=True  # 自动解码返回值为字符串
)

async def get_redis() -> AsyncGenerator[Redis, None]:
    redis = Redis(connection_pool=_pool, encoding="utf8", db=0)
    try:
        yield redis
    finally:
        await redis.aclose()  # 异步关闭连接
